import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:get/get_state_manager/get_state_manager.dart';
import 'package:yp/Xwidgets/index.dart';
import 'package:yp/config/index.dart';
import 'package:yp/helper.dart';
import 'package:yp/pages/mall_app/draw_bill/state.dart';
import 'package:yp/router.dart';

import 'logic.dart';
import 'widget/text_input.dart';

class DrawBillPage extends StatefulWidget {
  DrawBillPage({Key key}) : super(key: key);

  @override
  _DrawBillPageState createState() {
    return _DrawBillPageState();
  }
}

class _DrawBillPageState extends State<DrawBillPage> {
  final DrawBillState state = Get.find<DrawBillController>().state;
  final DrawBillController logic = Get.put(DrawBillController());
  // DrawBillController controller = Get.put(DrawBillController());
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Color(0xfff5f5f5),
      appBar: AppBar(
        centerTitle: true,
        backgroundColor: Color(0xffEA5504),
        elevation: 0,
        title: Text('开具发票'),
      ),
      body: GetBuilder<DrawBillController>(
        init: DrawBillController(),
        builder: (logic) {
          return Container(
            child: ListView(
              children: [
                // 开具发票
                Container(
                  padding: EdgeInsetsDirectional.all(20.0),
                  child: Column(
                    children: [
                      Container(
                        alignment: Alignment.centerLeft,
                        child: _title('开具发票'),
                      ),
                      Container(
                        margin: EdgeInsets.symmetric(vertical: 30.w),
                        child: Row(
                          children: [
                            InkWell(
                              onTap: () {
                                logic.changeDrawBillSelect(context, true);
                              },
                              child: Row(
                                children: [
                                  Image(
                                    image: AssetImage(state.isNeedDrawBill
                                        ? 'assets/images/selectIcon1.png'
                                        : 'assets/images/select1.png'),
                                    width: 48.w,
                                  ),
                                  SizedBox(
                                    width: 10,
                                  ),
                                  Text(
                                    '电子发票',
                                    style: TextStyle(
                                      fontSize: 28.sp,
                                      color: Color(0xff000000),
                                    ),
                                  ),
                                ],
                              ),
                            ),
                            SizedBox(
                              width: 20,
                            ),
                            InkWell(
                              onTap: () {
                                logic.changeDrawBillSelect(context, false);
                              },
                              child: Row(
                                children: [
                                  Image(
                                    image: AssetImage(!state.isNeedDrawBill
                                        ? 'assets/images/selectIcon1.png'
                                        : 'assets/images/select1.png'),
                                    width: 48.w,
                                  ),
                                  SizedBox(
                                    width: 10,
                                  ),
                                  Text(
                                    '不需要发票',
                                    style: TextStyle(
                                      fontSize: 28.sp,
                                      color: Color(0xff000000),
                                    ),
                                  ),
                                ],
                              ),
                            ),
                          ],
                        ),
                      ),
                      SizedBox(
                        height: 30.w,
                      ),
                      YpLine(
                        color: Color(0xffcccccc),
                      ),
                    ],
                  ),
                ),
                // 发票内容
                state.isNeedDrawBill ? _needDrawBill() : Container(),
                // 售票人信息
                // 发票内容
                InkWell(
                  onTap: () {
                    logic.submit(context);
                  },
                  child: Container(
                    margin: EdgeInsets.only(top: 60.w),
                    color: Config.mainColor,
                    alignment: Alignment.center,
                    height: setWidth(200.w),
                    width: MediaQuery.of(context).size.width,
                    child: Text(
                      '确定',
                      style: TextStyle(
                        color: Color(0xffffffff),
                        fontSize: 32.sp,
                      ),
                    ),
                  ),
                )
              ],
            ),
          );
        },
      ),
    );
  }

  // 统一标题
  Widget _title(String title) {
    return Container(
      child: Text(
        title,
        style: TextStyle(
          color: Color(0xff000000),
          fontSize: 34.w,
          fontWeight: FontWeight.bold,
        ),
      ),
    );
  }

  // 需要发票内容
  Widget _needDrawBill() {
    return Container(
      padding: EdgeInsets.only(left: 20.0),
      child: Column(
        children: [
          Container(
            alignment: Alignment.centerLeft,
            child: _title('发票抬头'),
          ),
          Container(
            margin: EdgeInsets.symmetric(vertical: 20.w),
            child: Row(
              children: [
                InkWell(
                  onTap: () {
                    logic.changeInvoiceHeade(context, true);
                  },
                  child: Container(
                    child: Row(
                      children: [
                        Image(
                          image: AssetImage(
                            state.isPersonalInvoice
                                ? 'assets/images/selectIcon1.png'
                                : 'assets/images/select1.png',
                          ),
                          width: 48.w,
                        ),
                        SizedBox(
                          width: 10,
                        ),
                        Text(
                          '个人/非企业',
                          style: TextStyle(
                            fontSize: 28.sp,
                            color: Color(0xff000000),
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
                SizedBox(
                  width: 20,
                ),
                InkWell(
                  onTap: () {
                    logic.changeInvoiceHeade(context, false);
                  },
                  child: Container(
                    child: Row(
                      children: [
                        Image(
                          image: AssetImage(!state.isPersonalInvoice
                              ? 'assets/images/selectIcon1.png'
                              : 'assets/images/select1.png'),
                          width: 48.w,
                        ),
                        SizedBox(
                          width: 10,
                        ),
                        Text(
                          '企业',
                          style: TextStyle(
                            fontSize: 28.sp,
                            color: Color(0xff000000),
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
              ],
            ),
          ),
          // !state.isPersonalInvoice ? _companyBill() : Container(),
          !state.isPersonalInvoice ? _companyBill() : _personalBill(),
          _public(),
        ],
      ),
    );
  }

  // 企业发票抬头
  Widget _companyBill() {
    return Column(
      children: [
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Container(
              margin: EdgeInsets.only(top: 20.w),
              child: Row(
                children: [
                  Container(
                    width: 180.w,
                    child: Text(
                      '发票抬头',
                      style: TextStyle(
                        color: Color(0xff939393),
                        fontWeight: FontWeight.bold,
                        fontSize: 29.sp,
                      ),
                    ),
                  ),
                  Container(
                    width: 350.w,
                    child: Container(
                      child: TextField(
                        controller: state.dillHeader,
                        enabled: true,
                        textAlignVertical: TextAlignVertical.center,
                        textAlign: TextAlign.start,
                        style: TextStyle(
                          fontSize: 26.sp,
                        ),
                        decoration: InputDecoration(
                          isDense: true,
                          contentPadding: EdgeInsets.all(0),
                          hintText: '请输入发票抬头',
                          hintStyle: TextStyle(
                            fontSize: 26.sp,
                            color: Color(0xffcccccc),
                          ),
                          border: InputBorder.none,
                        ),
                      ),
                    ),
                  ),
                ],
              ),
            ),
            InkWell(
              onTap: () {
                chooseInvoice();
              },
              child: Container(
                margin: EdgeInsets.only(right: 20.w),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.end,
                  children: [
                    ConstrainedBox(
                      constraints: BoxConstraints(
                        maxWidth: 250.w,
                      ),
                      child: Text(
                        "请选择",
                        style: TextStyle(
                          fontSize: 28.sp,
                          color: EsColors.Primary,
                        ),
                        maxLines: 1,
                        overflow: TextOverflow.ellipsis,
                      ),
                    ),
                    Icon(
                      Icons.arrow_forward_ios_rounded,
                      color: EsColors.Primary,
                      size: 14,
                    ),
                  ],
                ),
              ),
            ),
          ],
        ),
        SizedBox(
          height: 20.w,
        ),
        YpLine(),
        TextInputWidget(
          title: "企业税号",
          hintText: "请输入纳税人识别号",
          controller: state.controller2,
        ),
        TextInputWidget(
          title: "企业地址",
          hintText: "请输入企业注册地址",
          controller: state.controller3,
        ),
        TextInputWidget(
          title: "企业电话",
          hintText: "请输入企业注册电话",
          controller: state.controller4,
        ),
        TextInputWidget(
          title: "开户银行",
          hintText: "请输入企业开户银行",
          controller: state.controller5,
        ),
        TextInputWidget(
          title: "银行账号",
          hintText: "请输入企业银行账号",
          controller: state.controller6,
        ),
      ],
    );
  }

  chooseInvoice() {
    Get.toNamed(RouteConfig.invoiceHeaderManagement, arguments: {
      'chooseType': true,
    }).then((value) {
      print('发票抬头 选择事件 返回！！！ value = $value');
      if (value != null) {
        state.isPersonalInvoice = value['invoice_type'] == 1;
        state.dillHeader.text = value['invoice_title'];
        value['invoice_type'] == 1
            ? state.billType = '(个人/非企业)'
            : state.billType = '(企业)';
        state.controller2.text = value['consumer_tax_account'];
        value['invoice_type'] == 1
            ? state.billType = '(个人/非企业)'
            : state.billType = '(企业)';
        state.controller3.text = value['consumer_addr'];
        state.controller4.text = value['consumer_telephone'];
        state.controller5.text = value['consumer_bank_name'];
        state.controller6.text = value['consumer_bank_account'];
        setState(() {});
      }
    });
  }

  // 个人发票抬头
  Widget _personalBill() {
    return Container(
      margin: EdgeInsets.symmetric(vertical: 30.w),
      child: Column(
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Expanded(
                child: Container(
                  child: Row(
                    children: [
                      Container(
                        width: setWidth(260.w),
                        child: Text(
                          '发票抬头',
                          style: TextStyle(
                            color: Color(0xff939393),
                          ),
                        ),
                      ),
                      Expanded(
                        child: Container(
                          margin: EdgeInsets.only(left: 20.w),
                          // width: setWidth(500.w),
                          child: Container(
                            // width: 280.w,
                            child: TextField(
                              controller: state.dillHeader,
                              enabled: true,
                              textAlignVertical: TextAlignVertical.center,
                              style: TextStyle(
                                fontSize: 26.sp,
                                color: Colors.black,
                              ),
                              textAlign: TextAlign.start,
                              decoration: InputDecoration(
                                isDense: true,
                                contentPadding: EdgeInsets.all(0),
                                hintText: '请输入发票抬头',
                                hintStyle: TextStyle(
                                  fontSize: 26.sp,
                                  color: Color(0xffcccccc),
                                ),
                                border: InputBorder.none,
                              ),
                            ),
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
              ),
              InkWell(
                  onTap: () {
                    chooseInvoice();
                  },
                  child: Container(
                    margin: EdgeInsets.only(right: 20.w),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.end,
                      children: [
                        ConstrainedBox(
                          constraints: BoxConstraints(
                            maxWidth: 250.w,
                          ),
                          child: Text(
                            "请选择",
                            style: TextStyle(
                              fontSize: 28.sp,
                              color: EsColors.Primary,
                            ),
                            maxLines: 1,
                            overflow: TextOverflow.ellipsis,
                          ),
                        ),
                        Icon(
                          Icons.arrow_forward_ios_rounded,
                          color: EsColors.Primary,
                          size: 14,
                        ),
                      ],
                    ),
                  )),
            ],
          ),
          SizedBox(
            height: 30.w,
          ),
          YpLine(
            color: Color(0xffcccccc),
          ),
        ],
      ),
    );
  }

  // 发票公共部分
  Widget _public() {
    return Column(
      children: [
        Container(
          margin: EdgeInsets.only(top: 20.w),
          alignment: Alignment.centerLeft,
          child: _title('收票人邮箱'),
        ),
        Container(
          margin: EdgeInsets.symmetric(vertical: 30.w),
          child: TextField(
            controller: state.eMailAddress,
            enabled: true,
            textAlignVertical: TextAlignVertical.center,
            textAlign: TextAlign.start,
            decoration: InputDecoration(
              isDense: true,
              contentPadding: EdgeInsets.all(0),
              hintText: '请输入您的邮箱',
              hintStyle: TextStyle(
                fontSize: 28.sp,
                color: Color(0xff999999),
              ),
              border: InputBorder.none,
            ),
          ),
        ),
        SizedBox(
          height: 30.w,
        ),
        YpLine(
          color: Color(0xffcccccc),
        ),
        Container(
          margin: EdgeInsets.only(top: 20.w),
          alignment: Alignment.centerLeft,
          child: _title('发票内容'),
        ),
        InkWell(
          onTap: () {
            logic.ChangeCommodityDetail(context);
          },
          child: Container(
            child: Row(
              children: [
                Image(
                  image: AssetImage(
                    state.isNeedCommodityDetail
                        ? 'assets/images/selectIcon1.png'
                        : 'assets/images/select1.png',
                  ),
                  width: 48.w,
                ),
                SizedBox(
                  width: 10,
                ),
                Container(
                  margin: EdgeInsets.only(top: 24.w, bottom: 24.w),
                  child: Text(
                    '商品明细',
                    style: TextStyle(
                      fontSize: 28.sp,
                      color: Color(0xff000000),
                    ),
                  ),
                ),
              ],
            ),
          ),
        ),
        Container(
          margin: EdgeInsets.only(top: 20.w),
          child: Text(
            '商品明细：发票内容将显示商品名称与价格信息，发票金额为时机支付金额，不含虚拟资产、优惠券等扣减金额',
            style: TextStyle(
              fontSize: 28.sp,
              color: Color(0xff999999),
            ),
          ),
        )
      ],
    );
  }
}
